//
//  BaseDomain.m
//  Bank_IOS_Test
//
//  Created by carlos on 12-11-11.
//  Copyright (c) 2012年 carlos. All rights reserved.
//

#import "BaseDomain.h"
#import <objc/runtime.h>
#import "NSObject+AutoDescription.h"
@implementation BaseDomain

- (id)initWithDictionary:(NSDictionary *)dictionary {
    self = [super init];
    if (self) {
        
    }
    return self;
}

+ (void)setObject:(id)object withDictionary:(NSDictionary *)dictionary {
    
    Class classType = [object class];
    // Add Information about Current Properties
    NSUInteger		  property_count;
    objc_property_t * property_list = class_copyPropertyList(classType, &property_count); // Must Free, later
    
    for (int i = property_count - 1; i >= 0; --i) { // Reverse order, to get Properties in order they were defined
        objc_property_t property = property_list[i];
        
        // For Eeach property we are loading its name
        const char * property_name = property_getName(property);
        NSString * propertyName = [NSString stringWithCString:property_name encoding:NSASCIIStringEncoding];
        
        if (propertyName) { // and if name is ok, we are getting value using KVC
            if ([BaseDomain isDictionary:dictionary HasKey:propertyName]) {
                id value = [dictionary objectForKey:propertyName];
                [object setValue:value forKey:propertyName];
            }
            // format of result items: p1 = v1; p2 = v2; ...
        }
    }
    free(property_list);//Clean up

}

+ (BOOL)isDictionary:(NSDictionary *)dictionary HasKey:(NSString *)key {
    for (NSString *inkey in dictionary.allKeys) {
        if ([key isEqualToString:inkey]) {
            return YES;
            
//            const char *propertyName = [key cStringUsingEncoding:NSASCIIStringEncoding];
//            class_getProperty([self class], propertyName);
        }
        
    }
    return NO;
}

- (NSString *)description {
    return [self autoDescription];
}

- (NSMutableDictionary*)getTestModuleDataTypeForAttrs
{
    return nil;
}
@end
